package com.biz.primus.ms.order.promotion;

import com.biz.primus.base.enums.PromotionTypeEnum;
import com.biz.primus.model.order.vo.request.OrderItemRequestVO;
import com.biz.primus.model.order.vo.request.OrderRequestVO;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.model.promotion.vo.ProductCutPriceVo;
import com.biz.primus.ms.order.promotion.helper.OrderPromotionHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 降价拍促销
 * <P>CUT_PRICE_SALE("降价拍", "拍")</P>
 *
 * @author Tiger Feng
 * @date 2017年11月10日
 */
public class DepreciatePromotionHandler extends AbstractPromotionHandler {
    private DepreciatePromotionHandler() {
    }

    public DepreciatePromotionHandler(
            OrderPromotionDetailVo orderPromotionDetailVo,
            OrderRequestVO orderRequestVO,
            OrderPromotionHelper orderPromotionHelper) {
        super(orderPromotionDetailVo, orderRequestVO, orderPromotionHelper);
    }

    @Override
    public boolean checkPromotionType() {
        return orderPromotionDetailVo.getPromotionType() == PromotionTypeEnum.CUT_PRICE_SALE;
    }

    @Override
    public void count() {
        Map<Long, ProductCutPriceVo> productCutPriceVoMap = getMapForProductCutPrices();
        for(OrderItemRequestVO itemRequestVO : orderRequestVO.getItems()){
            ProductCutPriceVo productCutPriceVo = productCutPriceVoMap.get(itemRequestVO.getProductId());
            // 当前订单商品不再促销中，则跳过
            if(productCutPriceVo == null){
                continue;
            }
            // 设置当前商品促销减免金额
            itemRequestVO.setDiscountPromotionMoney(productCutPriceVo.getCutPrice());
            // 设置商品最终价
            itemRequestVO.setFinalPrice(itemRequestVO.getFinalPrice() - productCutPriceVo.getCutPrice());
        }
    }

    /**
     * 将降价拍商品列表转Map
     * @return
     */
    private Map<Long, ProductCutPriceVo> getMapForProductCutPrices(){
        List<ProductCutPriceVo> productCutPrices = orderPromotionDetailVo.getProductCutPrices();
        if(productCutPrices == null || productCutPrices.isEmpty()){
            return new HashMap<>(1);
        }
        return productCutPrices.stream().collect(Collectors.toMap(x -> x.getProductId(), x -> x));
    }
}
